<?php

/**
 * 客户列表 todo
 * @desc 实现客户的添加，管理，修改，删除等操作
 */
class Api_App_Apply extends PhalApi_Api {
    public function getRules() {
        return [
            'add'           => [
                'apply_id'  => ['name' => 'apply_id', 'require' => true, 'explain' => 'POST数据', 'desc' => '客户唯一标识 apply_id'],
                'uname'    => ['name' => 'uname', 'require' => true, 'explain' => 'POST数据', 'desc' => '真实姓名'],
                'contact' => ['name' => 'contact', 'require' => true, 'explain' => 'POST数据', 'desc' => '联系方式'],
                'ext'     => ['name' => 'ext', 'require' => true, 'explain' => 'POST数据', 'desc' => '扩展信息'],
            ],
            'info'          => [
                //'token' => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
                'apply_id'   => ['name' => 'apply_id', 'require' => true, 'explain' => 'POST数据', 'desc' => '客户唯一标识 apply_id']
            ],

            'delete'        => [
                //'token' => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
                'apply_id'    => ['name' => 'apply_id', 'require' => true, 'explain' => 'POST数据', 'desc' => '客户唯一标识 apply_id']
            ],
            'lists'         => [
                'admin' => ['name' => 'admin', 'type' => 'string', 'min' => 1, 'max' => 12, 'require' => true, 'desc' => ' 所属客户的 openid '],
            ],
            'edit'          =>  [
                'apply_id'    => ['name' => 'apply_id', 'require' => true, 'explain' => 'POST数据', 'desc' => '客户唯一标识 apply_id'],
                'uname'    => ['name' => 'uname', 'require' => true, 'explain' => 'POST数据', 'desc' => '真实姓名'],
                'contact' => ['name' => 'contact', 'require' => true, 'explain' => 'POST数据', 'desc' => '联系方式'],
                'ext'     => ['name' => 'ext', 'require' => true, 'explain' => 'POST数据', 'desc' => '扩展信息'],
            ],
        ];
    }

    /**
     * 添加用户报名数据 todo
     * @desc
     * @param string $openid 微信openid
     * @param string $uname 姓名
     * @param string $contact 联系方式
     * @param string $pid 来源项目名称id
     * @param array $ext 扩展数据
     *
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array data 返回数据
     * @return string msg 消息&nbsp;接口返回消息数据对应说明：<br>DUPLICATE=>数据重复<br>INSERT&nbsp;FAIL=>插入数据失败<br>MOBILE&nbsp;NO&nbsp;FORMAT&nbsp;EXCEPTION=>电话号码格式有误<br>NAME&nbsp;FORMAT&nbsp;EXCEPTION=>姓名格式有误
     * @return string version 版本信息
     * @return string timestamp 返回时间戳
     */
    /**
     * 根据 apply_id 获取客户数据 todo
     * @desc
     * @return mixed
     * @throws PhalApi_Exception
     */
    public function Info() {
        $domain = new Domain_App_Apply();
        $data = [
            'apply_id' => $this->apply_id,
        ];
        return $domain->getApplyByApplyId($data['apply_id']);
    }

//    /**
//     * 查询客户资料列表
//     * @desc
//     */
//
//    public function Plists(){
//        $domain = new Domain_App_Apply();
//        return $domain->getApplyList();
//    }

    /**
     * 添加客户信息资料 todo
     * @desc
     */
    public function Add() {
        $domain = new Domain_App_Apply();
        $data = [
            'ctime'   => DI()->helper->getTime(),
            'ip'      => DI()->helper->getClientIp(),
            'apply_id'  => $this->apply_id,
            'uname'   => $this->uname,
            'contact' => $this->contact,
            'ext'     => $this->ext,
        ];
        $info = $domain->addApply($data);
        if($info){
            return array(
                'version' => 'version',
                'timestamp' => strtotime("now"),
            );
        }

    }
    /**
     * 删除数据 todo
     * @desc
     */
    public function Delete() {
        $data = array(
            'apply_id' => $this->apply_id,
        );
        $domain = new Domain_App_Apply();
        $info = $domain->deleteApplyByOpenid($data['apply_id']);
        if($info){
            return array(
                'message' => '删除成功',
            );
        }
        return array(
            'message' => '删除失败',
        );
    }
    /**
     * 更新 客户资料 todo
     * @desc
     */
    public function Edit(){
        $data = array(
            'ctime'   => DI()->helper->getTime(),
            'ip'      => DI()->helper->getClientIp(),
            'apply_id'  => $this->apply_id,
            'uname'   => $this->uname,
            'contact' => $this->contact,
            'ext'     => $this->ext
        );
        $domain = new Domain_App_Apply();
        $info = $domain ->edit($data,$data['apply_id']);
        if($info){
            return array(
                'message' => '更新成功',
            );
        }
        return array(
            'message' => '更新失败',
        );
    }



    /**
     * 获取客户数据列表 todo
     * @desc
     */
    public function lists() {
        $domain = new Domain_App_Apply();
        return $domain->getApplyList();
    }
}
